// stores/counter.js
import request from '../utils/request'
import { defineStore } from 'pinia'
import { treeDataTranslate } from "../utils/treeDataTranslate"
import { ElMessage } from 'element-plus'

export const address = defineStore('address', {
    state: () => {
        return {
            address_list: []
        }
    },

    actions: {
        get_list() {
            request({
                url: `/admin/area/list?t=1669174766468&current=1&size=10`,
                method: "get"
            }).then(res => {
                this.address_list = treeDataTranslate(res.data, "areaId", "parentId");
                console.log(this.address_list, "address_list");

            })
        },
        add_list(obj: any) {
            console.log(obj, "obj");

            request({
                url: `/admin/area`,
                method: "post",
                data: {
                    areaId: 0,
                    areaName: obj.address_user,
                    aseas: null,
                    level: obj.level,
                    parentId: obj.parentId,
                    t: new Date().getTime()
                }
            }).then(res => {
                if (res.status == 200) {
                    ElMessage({
                        message: '操作成功',
                        type: 'success',
                    })
                    this.get_list()
                } else {
                    ElMessage({
                        message: '操作失败',
                        type: 'error',
                    })
                }
            })
        },
        edit_list(obj: any) {
            request({
                url: `admin/area`,
                method: "put",
                data: {
                    areaId: obj.areaId,
                    areaName: obj.address_user,
                    aseas: null,
                    level: obj.level,
                    parentId: obj.parentId,
                    t: new Date().getTime()
                }
            }).then(res => {
                if (res.status == 200) {
                    ElMessage({
                        message: '操作成功',
                        type: 'success',
                    })
                    this.get_list()
                } else {
                    ElMessage({
                        message: '操作失败',
                        type: 'error',
                    })
                }
            })
        },
        del_list(obj: any) {
            console.log(obj, "obj");

            request({
                url: `/admin/area/${obj.data.areaId}`,
                method: "delete",
                data: { t: new Date().getTime() }
            }).then(res => {

                if (res.status == 200) {
                    ElMessage({
                        message: '操作成功',
                        type: 'success',
                    })
                    this.get_list()
                } else {
                    ElMessage({
                        message: '操作失败',
                        type: 'error',
                    })
                }
            })
        }
    },

})